package com.he.hsdyc.service.impl;

import com.he.hsdyc.common.Constants;
import com.he.hsdyc.common.utils.ValidateUtils;
import com.he.hsdyc.config.ServerProperties;
import com.he.hsdyc.service.ServiceService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ServiceServiceImpl implements ServiceService {

    @Autowired
    private ServerProperties wechatProperties;

    @Autowired
    @Qualifier("wxMpService_parent2")
    protected WxMpService wxMpService_parent2;


    @Override
    public void communicatePush(String stuName, Long stuId, String publishName, Long communicateId) throws UnsupportedEncodingException {
        stuName = URLDecoder.decode(stuName,"UTF-8");
        publishName = URLDecoder.decode(publishName,"UTF-8");

//        List<Map> parentBindInfo = serviceMapper.findParentBindInfoByStuId(stuId);
//        for (Map parentBindInfoTemp : parentBindInfo) {
//            Long parentId = Long.parseLong(parentBindInfoTemp.get("mainId").toString());
//            Map parentInfo = serviceMapper.findParentInfoById(parentId);
//            if (parentInfo!=null) {
//                String parentOpenId = parentInfo.get("openId").toString();
                String parentOpenId = "ohZJb0wTNsYQLCQ22leX_w4-FGEY";
                String templateId1 = "q-30ti4IxDR3EMDsq9Kawy5MVKSALYEgzOUppVHpdt4";//测试号环境
//                String templateId1 = "c4HZZbEQ6_x1fHS6wRmdO7pKW1BvtI17kJ4CsqxDzuM";//正式环境
//                String templateId1 = "EjR00RmMm9PXkdH_K9FGwY0ELRMLQRsU_jy1ypdq_nc";//测试环境
                List<Map<String, String>> pushData = new ArrayList<Map<String, String>>();

//                String firstName = "沟通回访通知";
//                Map<String, String> pushMap = new HashMap<>();
//                pushMap.put("name", "first");
//                pushMap.put("color", "#173177");
//                pushMap.put("value", firstName);
//                pushData.add(pushMap);

                Map<String, String> pushMap2 = new HashMap<>();
                pushMap2.put("name", "keyword1");
                pushMap2.put("color", "#173177");
                pushMap2.put("value", stuName);
                pushData.add(pushMap2);

                Map<String, String> pushMap3 = new HashMap<>();
                pushMap3.put("name", "keyword2");
                pushMap3.put("color", "#173177");
                pushMap3.put("value", publishName);
                pushData.add(pushMap3);

                Date dt = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String temp_str = sdf.format(dt);


                Map<String, String> pushMap4 = new HashMap<>();
                pushMap4.put("name", "keyword3");
                pushMap4.put("color", "#173177");
                pushMap4.put("value", temp_str);
                pushData.add(pushMap4);

                Map<String, String> pushMap5 = new HashMap<>();
                pushMap5.put("name", "remark");
                pushMap5.put("color", "#173177");
                pushMap5.put("value", "您收到一条沟通回访,请点击查看！");
                pushData.add(pushMap5);

                String url = wechatProperties.getDomain() + "/communication/skip2PublishCom?openId="+
                        parentOpenId+"&stuId="+stuId+"&communicateId="+ communicateId;

                // 签到完成后发送微信公众号推送
                this.pushMessage(parentOpenId, templateId1, pushData, url);
//            }

//        }
    }

    public Map pushMessage(String openId, String templateId, List<Map<String, String>> data, String url) {
        Map result = new HashMap();
        try {

            WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();
            wxMpTemplateMessage.setToUser(openId);
            wxMpTemplateMessage.setTemplateId(templateId);
            if (url != null && !url.equals("")) {
                wxMpTemplateMessage.setUrl(url);
            }

            List<WxMpTemplateData> wxMpTemplateDataList = new ArrayList<>();
            for (int i = 0;i < data.size();i++) {
                Map temp = (Map)data.get(i);
                WxMpTemplateData wxMpTemplateData1 = new WxMpTemplateData();
                wxMpTemplateData1.setColor(temp.get("color").toString());
                wxMpTemplateData1.setName(temp.get("name").toString());
                wxMpTemplateData1.setValue(temp.get("value").toString());
                wxMpTemplateDataList.add(wxMpTemplateData1);
            }

            wxMpTemplateMessage.setData(wxMpTemplateDataList);
            String  resultMsg = wxMpService_parent2.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
            result.put("code", 200);
            result.put("msg", resultMsg);
        } catch (Exception e) {
            result.put("code", 400);
            result.put("msg", "推送失败");
            e.printStackTrace();
        }
        return result;
    }


















    @Override
    public void communicatePushMini(String stuName, Long stuId, String openId,String publishName, Long communicateId) throws UnsupportedEncodingException {
        stuName = URLDecoder.decode(stuName,"UTF-8");
        publishName = URLDecoder.decode(publishName,"UTF-8");

//        List<Map> parentBindInfo = serviceMapper.findParentBindInfoByStuId(stuId);
//        for (Map parentBindInfoTemp : parentBindInfo) {
//            Long parentId = Long.parseLong(parentBindInfoTemp.get("mainId").toString());
//            Map parentInfo = serviceMapper.findParentInfoById(parentId);
//            if (parentInfo!=null) {
        String parentOpenId = openId;
        String templateId1 = "q-30ti4IxDR3EMDsq9Kawy5MVKSALYEgzOUppVHpdt4";
//        String templateId1 = "c4HZZbEQ6_x1fHS6wRmdO7pKW1BvtI17kJ4CsqxDzuM";//正式环境
//                String templateId1 = "EjR00RmMm9PXkdH_K9FGwY0ELRMLQRsU_jy1ypdq_nc";//测试环境
        List<Map<String, String>> pushData = new ArrayList<Map<String, String>>();

        String firstName = "沟通回访通知";
        Map<String, String> pushMap = new HashMap<>();
        pushMap.put("name", "first");
        pushMap.put("color", "#173177");
        pushMap.put("value", firstName);
        pushData.add(pushMap);

        Map<String, String> pushMap2 = new HashMap<>();
        pushMap2.put("name", "keyword1");
        pushMap2.put("color", "#173177");
        pushMap2.put("value", stuName);
        pushData.add(pushMap2);

        Map<String, String> pushMap3 = new HashMap<>();
        pushMap3.put("name", "keyword2");
        pushMap3.put("color", "#173177");
        pushMap3.put("value", publishName);
        pushData.add(pushMap3);

        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String temp_str = sdf.format(dt);


        Map<String, String> pushMap4 = new HashMap<>();
        pushMap4.put("name", "keyword3");
        pushMap4.put("color", "#173177");
        pushMap4.put("value", temp_str);
        pushData.add(pushMap4);

        Map<String, String> pushMap5 = new HashMap<>();
        pushMap5.put("name", "remark");
        pushMap5.put("color", "#173177");
        pushMap5.put("value", "您收到一条沟通回访,请点击查看！");
        pushData.add(pushMap5);

        String url = wechatProperties.getDomain() + "/communication/skip2PublishCom?openId="+
                parentOpenId+"&stuId="+stuId+"&communicateId="+ communicateId;


        String pagePath = "pages/tabBar/home-school/communicate-detail/index?id="+ communicateId;
        // 签到完成后发送微信公众号推送
        this.pushParentMessage(parentOpenId, templateId1, pushData, url, Constants.appid_parent,pagePath);

    }




    //家长端推送
    private Map pushParentMessage(String openId, String templateId, List<Map<String, String>> data, String url,String appid,String pagePath) {
        if (ValidateUtils.isIllegalString(openId)) {
            return null;
        }
        Map result = new HashMap();
        try {
            WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();
            wxMpTemplateMessage.setToUser(openId);
            wxMpTemplateMessage.setTemplateId(templateId);
            if (url != null && !url.equals("")) {
                wxMpTemplateMessage.setUrl(url);
            }
            if (!ValidateUtils.isIllegalString(appid) && !ValidateUtils.isIllegalString(pagePath)){
                WxMpTemplateMessage.MiniProgram miniprogram = new WxMpTemplateMessage.MiniProgram();
                miniprogram.setAppid(appid);
                miniprogram.setPagePath(pagePath);
                miniprogram.setUsePath(false);
                wxMpTemplateMessage.setMiniProgram(miniprogram);
            }
            List<WxMpTemplateData> wxMpTemplateDataList = new ArrayList<>();
            for (int i = 0;i < data.size();i++) {
                Map temp = (Map)data.get(i);
                WxMpTemplateData wxMpTemplateData1 = new WxMpTemplateData();
                wxMpTemplateData1.setColor(temp.get("color").toString());
                wxMpTemplateData1.setName(temp.get("name").toString());
                wxMpTemplateData1.setValue(temp.get("value").toString());
                wxMpTemplateDataList.add(wxMpTemplateData1);
            }
            wxMpTemplateMessage.setData(wxMpTemplateDataList);
            String resultMsg = "";
            resultMsg = wxMpService_parent2.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
            result.put("code", 200);
            result.put("msg", resultMsg);
        } catch (Exception e) {
            result.put("code", 400);
            result.put("msg", "推送失败");
            e.printStackTrace();
        }
        return result;
    }

}
